package messagePacket;



import java.io.UnsupportedEncodingException;
import java.util.HashMap;

import Util.Util;
import io.netty.channel.Channel;
import server.ChattingServeHandler;

/**
 * @author gg_boy
 * @version $Rev$
 * @des ${TODO}
 * @updateAuther $Auther$
 * @updateDes ${TODO}
 */
public class DeleteFriendPacket extends Packet {
    private String sendUser;
    private String deleteUserName;

    public DeleteFriendPacket(String sendUser, String deleteUserName) {
        this.sendUser = sendUser;
        this.deleteUserName = deleteUserName;
    }
    public DeleteFriendPacket(){}

    @Override
    public int getPacketType() {
        return super.getPacketType();
    }

    @Override
    public void setPacketType(int packetType) {
        super.setPacketType(packetType);
    }

    
    public String getSendUser() {
		return sendUser;
	}
	public void setSendUser(String sendUser) {
		this.sendUser = sendUser;
	}
	public String getDeleteUserName() {
		return deleteUserName;
	}
	public void setDeleteUserName(String deleteUserName) {
		this.deleteUserName = deleteUserName;
	}
	@Override
    public void decode(byte[] buffer) throws UnsupportedEncodingException {
        int start_pos = 4;
        // 获取长度
        int length = Util.bytes2int(buffer, start_pos);
        start_pos += 4;

        this.sendUser = new String(buffer, start_pos, length, "UTF-8");
        start_pos += length;


        length = Util.bytes2int(buffer, start_pos);

        start_pos += 4;
        this.deleteUserName = new String(buffer, start_pos, length, "UTF-8");
    }

    @Override
    public byte[] encode() throws UnsupportedEncodingException {
        int sendUserLength = this.sendUser.getBytes("UTF-8").length;
        int receiveUserLength = this.deleteUserName.getBytes("UTF-8").length;
        int totalLength = 4 + 4 + sendUserLength + 4 + receiveUserLength;
        byte[] buffer = new byte[totalLength];
        int offset = 0;

        // add packet type to buffer
        System.arraycopy(Util.int2bytes(Util.PACKET_DELETE_FRIEND), 0, buffer, offset, 4);
        offset += 4;

        // the four bytes store send user name length
        System.arraycopy(Util.int2bytes(sendUserLength), 0, buffer, offset, 4);
        offset += 4;

        // copy send user to buffer
        System.arraycopy(sendUser.getBytes("UTF-8"), 0, buffer, offset, sendUserLength);
        offset += sendUserLength;

        // the four bytes store receive user name length
        System.arraycopy(Util.int2bytes(receiveUserLength), 0, buffer, offset, 4);
        offset += 4;

        //copy receive user to buffer
        System.arraycopy(this.deleteUserName.getBytes("UTF-8"), 0, buffer, offset, receiveUserLength );
        offset += receiveUserLength;
        return buffer;
    }

    @Override
    public void process(Channel channel, Packet packet) {
    	DeleteFriendPacket deleteFriendPacket = (DeleteFriendPacket) packet;
    	if(deleteFriend(deleteFriendPacket)) {
    		MessagePacket chatMessage = new MessagePacket("服务器", deleteFriendPacket.getSendUser(),
					"删除好友"+deleteFriendPacket.getDeleteUserName()+"成功", 7);
			channel.writeAndFlush(chatMessage);
			MessagePacket chatMessage2 = new MessagePacket("服务器", deleteFriendPacket.getSendUser(),
					deleteFriendPacket.getSendUser()+"将你移出好友列表", 7);
			ChattingServeHandler.uc.getChannel(deleteFriendPacket.getDeleteUserName()).writeAndFlush(chatMessage2);
    	}else {
    		MessagePacket chatMessage = new MessagePacket("服务器", deleteFriendPacket.getSendUser(),
					"删除好友"+deleteFriendPacket.getDeleteUserName()+"成功", 7);
    		channel.writeAndFlush(chatMessage);
    	}
    }
    
    private boolean deleteFriend(DeleteFriendPacket cmsg) {
		for (HashMap.Entry<String, HashMap<Integer, String>> friend : ChattingServeHandler.friends.entrySet()) {
			if(friend.getKey().equals(cmsg.getSendUser()) ) {
				for (HashMap.Entry<Integer, String> f : friend.getValue().entrySet()) {
					System.out.println(f.getValue());
					if(f.getValue().equals(cmsg.getDeleteUserName())) {
						friend.getValue().remove(f.getKey());
						break;
					}
				}
			}else if(friend.getKey().equals(cmsg.getDeleteUserName())) {
				for (HashMap.Entry<Integer, String> f : friend.getValue().entrySet()) {
					System.out.println(f.getValue());
					if(f.getValue().equals(cmsg.getSendUser())) {
						friend.getValue().remove(f.getKey());
						return true;
					}
				}
			}
		}
		return false;
	}
    @Override
    public String toString() {
        return super.toString();
    }
}
